package com.centaline.androidsalesblog.overlay.local;

import android.content.Context;
import android.os.Bundle;

import com.baidu.mapapi.map.BaiduMap;
import com.baidu.mapapi.map.BitmapDescriptorFactory;
import com.baidu.mapapi.map.Marker;
import com.baidu.mapapi.map.MarkerOptions;
import com.baidu.mapapi.map.Overlay;
import com.baidu.mapapi.map.OverlayOptions;
import com.centaline.androidsalesblog.base.BaseLevelOverlay;
import com.centaline.androidsalesblog.constants.ExtraConstant;
import com.centaline.androidsalesblog.iml.BaiduMarkerClick;
import com.centaline.androidsalesblog.model.businissobject.NewEstMainMapBo;

import java.util.ArrayList;
import java.util.Locale;

/**
 * 新盘区域板块
 * Created by guilin on 16/4/13.
 */
public class NewEstRegionLevelOverlay extends BaseLevelOverlay<NewEstMainMapBo> {

    public NewEstRegionLevelOverlay(Context context, BaiduMap baiduMap, ArrayList<NewEstMainMapBo> data,
                                    BaiduMarkerClick baiduMarkerClick) {
        super(context, baiduMap, data, baiduMarkerClick);
    }

    @Override
    public void mapStatusChange() {
        /*检查显示条件*/
        if (checkAll())
            return;
        ArrayList<Overlay> tempOverlay = new ArrayList<>();
        ArrayList<String> keys = new ArrayList<>();
        for (Overlay overlay : overlayList) {
            if (overlay instanceof Marker) {
                Marker marker = (Marker) overlay;
                NewEstMainMapBo newEstMainMapBo = overlay.getExtraInfo().getParcelable(ExtraConstant.MAP_EXTRA_INFO);
                if (newEstMainMapBo != null &&
                        baiduMap.getMapStatusLimit().contains(marker.getPosition())) {//已经包含
                    keys.add(newEstMainMapBo.getGscopId());
                    tempOverlay.add(overlay);
                } else {
                    overlay.remove();
                }
            }
        }
        overlayList.clear();
        overlayList.addAll(tempOverlay);
        for (NewEstMainMapBo newEstMainMapBo : data) {
            if (!keys.contains(newEstMainMapBo.getGscopId()) &&
                    isInBound(newEstMainMapBo)) {
                overlayList.add(baiduMap.addOverlay(getMarkerOptions(newEstMainMapBo)));
            }
        }
    }

    @Override
    protected OverlayOptions getMarkerOptions(NewEstMainMapBo newEstMainMapBo) {
        Bundle bundle = new Bundle();
        bundle.putParcelable(ExtraConstant.MAP_EXTRA_INFO, newEstMainMapBo);
        atv_content.setText(String.format(Locale.CHINA, "%s\n%d",
                newEstMainMapBo.getName().trim(), newEstMainMapBo.getCount()));
        return new MarkerOptions()
                .position(newEstMainMapBo.getLatLng())
                .extraInfo(bundle)
                .icon(BitmapDescriptorFactory.fromView(view));
    }

    @Override
    protected boolean isInBound(NewEstMainMapBo newEstMainMapBo) {
        return baiduMap.getMapStatusLimit().contains(newEstMainMapBo.getLatLng());
    }
}
